﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jumpton.SFA.Server.BusinessRule.MasterData;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;

/// <summary>
/// 付款条款维护
/// Update By:Steven Jiang
/// UpdateTime：2008-12-10 15：30
/// Description：
///     1. 修改编辑功能在本页面
///     2. 支付类型从字典表提取
/// </summary>
public partial class Pay_Term_List : PageBase
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    private void BindGrid()
    {
        gvChannelType.DataSource = new PayItemManager().GetCourseTable(this.MyStateManager.DomainID);
        gvChannelType.DataBind();
    }

    //保存数据
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            PayItemManager payManager = new PayItemManager();
            PAY_TERMS payTerm;
            TextBox ctl = DetailsView1.FindControl("txtCode") as TextBox;

            if (DetailsView1.CurrentMode == DetailsViewMode.Insert)  //新增
            {
                payTerm = new PAY_TERMS();
            }
            else //编辑
            {
                payTerm = payManager.Load(int.Parse(gvChannelType.SelectedDataKey.Value.ToString()));
            }

            payTerm.SAPCODE = ctl.Text;

            ctl = DetailsView1.FindControl("txtDescription") as TextBox;
            payTerm.DESCRIPTION = ctl.Text;

            ctl = DetailsView1.FindControl("txtPercent") as TextBox;
            payTerm.PERCENT = decimal.Parse(ctl.Text);
            
            ctl = DetailsView1.FindControl("txtDueDays") as TextBox;
            payTerm.DUEDAYS = decimal.Parse(ctl.Text);

            DropDownList ddlPayType = DetailsView1.FindControl("ddlPayType") as DropDownList;
            payTerm.TYPE = int.Parse(ddlPayType.SelectedValue);

            CheckBox ckbIsValid = DetailsView1.FindControl("ckbIsValid") as CheckBox;
            payTerm.ACTIVE = ckbIsValid.Checked;
            payTerm.LAST_UPDATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
            payTerm.LAST_UPDATER = MyStateManager.Name;
            payTerm.DOMAINID = MyStateManager.DomainID;

            payManager.Save(payTerm);
            BindGrid();
            btnCancel_Click(sender, e);
        }
        catch (Exception ex)
        {
            ShowMessage(UpdatePanel1,ex.Message);
        }
    }


    protected void btnCancel_Click(object sender, EventArgs e)
    {
        editArea.Visible = false;
        back.Attributes.Remove("class");
    }
    protected void gvChannelType_SelectedIndexChanged(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Edit);
        DetailsView1.DataBind();
        SetStatus();

    }

   
    protected void DetailsView1_DataBound(object sender, EventArgs e)
    {
        DropDownList ddlPayType = DetailsView1.FindControl("ddlPayType") as DropDownList;
        CheckBox chk = DetailsView1.FindControl("ckbIsValid") as CheckBox;

        if (ddlPayType != null)
        {

            ddlPayType.DataSource = new DictionaryItemsManager().GetItemList(DictionaryEnum.PaymentType, MyStateManager.DomainID);
            ddlPayType.DataTextField = "Name";
            ddlPayType.DataValueField = "dictionaryitemid";
            ddlPayType.DataBind();

            if (DetailsView1.DataItem != null)
            {
                ddlPayType.SelectedValue = ((PAY_TERMS)DetailsView1.DataItem).TYPE.ToString();
            }
            else
            {
                chk.Checked = true;
            }
        }
    }

    private void SetStatus()
    {
        editArea.Visible = true;
        editArea.Style.Add("width", "50%");
        back.Attributes.Add("class", "back");
    }


    //新增
    protected void btnNew_Click(object sender, EventArgs e)
    {
        DetailsView1.ChangeMode(DetailsViewMode.Insert);
        DetailsView1.DataBind();
        SetStatus();
    }
    protected void gvChannelType_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvChannelType.PageIndex = e.NewPageIndex;
        BindGrid();
    }
}
